//
//  Rchat.m
//  HandheldHospital
//
//  Created by 张鹏的电脑 on 16/3/3.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "RchatConteroller.h"
#import "AppDelegate.h"
#import "ZC_AsynchronousBlock.h"
#import <AVUser.h>
#import "DataManager.h"


#define kShared [DataManager defaultManager]

@interface RchatConteroller ()<RCIMUserInfoDataSource>


@end

@implementation RchatConteroller

-(instancetype)init{
    self = [super init];
    if (self ) {
        //添加聊天功能(单聊,群聊,聊天室...)
        [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),@(ConversationType_GROUP),@(ConversationType_DISCUSSION),@(ConversationType_GROUP)]] ;
        //添加群组
        //[self setCollectionConversationTypeArray:@[@(ConversationType_PRIVATE)]];
        
    }
    
    return self;
    
}

//点击会话列表中Cell的回调
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath{
    if (conversationModelType == RC_CONVERSATION_MODEL_TYPE_COLLECTION) {
        RchatConteroller *cc = [[RchatConteroller alloc] init];
        
        NSArray *array = [NSArray arrayWithObject:[NSNumber numberWithInt:model.conversationType]];
        [cc setDisplayConversationTypes:array];
        [cc setCollectionConversationType:nil];
        
        cc.isEnteredToCollectionViewController = YES;
        
        [self.navigationController pushViewController:cc animated:YES];
        
    }else if (model.conversationType == ConversationType_PRIVATE){
        
        RCConversationViewController *vc = [[RCConversationViewController alloc] init];
        vc.conversationType = model.conversationType;
        vc.targetId = model.targetId;
        
        vc.title = model.conversationTitle;
        [self.navigationController pushViewController:vc animated:YES];
    }else if((model.conversationType = ConversationType_GROUP) ){
        RCConversationViewController *gg = [[RCConversationViewController alloc] init];
        gg.targetId = @"qq";
        gg.conversationType = model.conversationType;
        
        gg.title = @"群聊天";
        [self.navigationController pushViewController:gg animated:YES];
        
        
    }
}

//修改 cell 的 title 颜色
- (void)willDisplayConversationTableCell:(RCConversationBaseCell *)cell
                             atIndexPath:(NSIndexPath *)indexPath{
    RCConversationModel *model = self.conversationListDataSource[indexPath.row];
    
    if (model.conversationType == ConversationType_PRIVATE) {
        RCConversationCell *conver = (RCConversationCell *)cell;
        conver.conversationTitle.textColor = [UIColor redColor];
       
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    
}


//实现协议方法
- (void)getUserInfoWithUserId:(NSString *)userId
                   completion:(void (^)(RCUserInfo *userInfo))completion{
    
    RCUserInfo *userIf = [[RCUserInfo alloc] init];
    userIf.userId = userId;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        AVQuery *query = [AVUser query];
        [query whereKey:@"username" containsString:userId];
        AVUser *user = [[query findObjects] firstObject];
        if([user[@"userType"] isEqualToString:@"医生"])
        {
            //判断如果是医生，从医生表中取出name
            AVQuery *q = [AVQuery queryWithClassName:@"SeekModel"];
            [q whereKey:@"currentUser" equalTo:user];
            userIf.name = [[q findObjects] firstObject][@"name"];
            AVFile *file = user[@"Picture"];
            userIf.portraitUri = file.url;
        }
        else
        {
            //如果不是医生，从用户表中取出username
            AVFile *file = user[@"Picture"];
            userIf.name = user[@"username"];
            userIf.portraitUri = file.url;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            return completion(userIf);
        });
    });
}






@end
